import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class PL0main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length != 2){
			System.out.println("Incorrect number of command line arguments; terminating execution.");
			System.exit(0);
		}
		
		File src = new File(args[0]);
		File out = new File(args[1]);
		
		/*
		 * Check names of the input files to make sure their different.
		 */
		if(src.toString() == out.toString()){
			System.out.println("The source and output file name must be different; terminating execution.");
			System.exit(0);
		}
		
		/*
		 * Check if output file already exists and that it's not a directory. If the
		 * output file does not exist, than one will be created.
		 */
		if(out.exists()){
			if(out.isDirectory()){
				System.out.println("Output file cannot be a directory; terminating execution.");
				System.exit(0);
			}
			System.out.println("Do you want to overwrite " + args[1].toString() + "?");
			System.out.println("Y/N/y/n");
			Scanner in = new Scanner(System.in);
			String ans = in.next();
			
			while(!(ans.equals("Y")) && !(ans.equals("N")) && !(ans.equals("y")) && !(ans.equals("n"))){
				System.out.println("Please enter either Y,N,y, or n");
				ans = in.next();
			}if ((ans.equals("N")) || (ans.equals("n"))){
				System.out.println("Please specify a new output file; terminating execution.");
				System.exit(0);
			}
			
		} else {
			try{
				if(!out.createNewFile()){
					System.out.println("Could not create a new output file; terminating execution.");
					System.out.println(0);
				}
			} catch (IOException e){
				System.out.println(e);
			}
		}
		
		/*
		 * Check if able to write to output file.
		 */
		if (!out.canWrite())
		{
			System.out.println("Output file cannot be overwritten; terminating execution.");
			System.exit(0);
		}
		
		/*
		 * Check if input file exists.
		 */
		if(!src.exists()){
			System.out.println("Input file does not exist; terminating execution.");
			System.exit(0);
		}
		
		/*
		 * Check if input file is not a directory.
		 */
		if(src.isDirectory()){
			System.out.println("Input file can not be a directory; terminating execution.");
			System.exit(0);
		}
		
		/*
		 * Check is input file is readable.
		 */
		if(!src.canRead()){
			System.out.println("Input file can not be read; terminating execution.");
			System.exit(0);
		}
		
		parser.parse(src, out);
	}

}
